<template>
  <a-form layout="inline" :model="searchParams" @finish="doSearch" style="margin-bottom: 10px">
    <a-form-item label="账号">
      <a-input v-model:value="searchParams.userAccount" placeholder="输入账号" />
    </a-form-item>
    <a-form-item label="用户名">
      <a-input v-model:value="searchParams.userName" placeholder="输入用户名" />
    </a-form-item>
    <a-form-item>
      <a-button type="primary" html-type="submit">搜索</a-button>
    </a-form-item>
  </a-form>

  <a-table
    :columns="columns"
    :data-source="dataList"
    :pagination="pagination"
    @change="doTableChange"
  >
    <template #bodyCell="{ column, record }">
      <template v-if="column.dataIndex === 'userAvatar'">
        <a-image :src="record.userAvatar" :width="120" />
      </template>
      <template v-else-if="column.dataIndex === 'userRole'">
        <div v-if="record.userRole === 'admin'">
          <a-tag color="green">管理员</a-tag>
        </div>
        <div v-else>
          <a-tag color="blue">普通用户</a-tag>
        </div>
      </template>
      <template v-else-if="column.dataIndex === 'createTime'">
        {{ dayjs(record.createTime).format('YYYY-MM-DD HH:mm:ss') }}
      </template>
      <template v-else-if="column.key === 'action'" scope="row">
        <a-button danger style="margin-right: 10px" @click="doDelete(record.id)">删除</a-button>
        <a-button type="primary" ghost style="margin-right: 10px">编辑</a-button>
        <a-button type="primary" @click="showModal(record.id)">详情</a-button>

      </template>
    </template>
  </a-table>
  <a-modal v-model:open="open" title="用户详情" footer="" destroy-on-close>
    <P>ID:{{ userData.id }}</P>
    <P>账号:{{ userData.userAccount }}</P>
    <P>用户昵称:{{ userData.userName }}</P>
    <P v-if="userData.userRole==='admin' ">
      用户角色:
      <a-tag color="green">管理员</a-tag>
    </P>
    <p v-else>
      用户角色:
      <a-tag color="blue">普通用户</a-tag>
    </p>

    <!--    <template v-if="userData.userAvatar === 'userAvatar'">-->
    <!--      <a-image :src="userData.userAvatar" :width="120" />-->
    <!--    </template>-->
    <P>
      <a-image :src="userData.userAvatar" :width="120" />
    </P>
    <P>创建时间:{{ dayjs(userData.createTime).format('YYYY-MM-DD HH:mm:ss') }}</P>
    <P>编辑时间:{{ dayjs(userData.updateTime).format('YYYY-MM-DD HH:mm:ss') }}</P>
  </a-modal>
</template>
<script lang="ts" setup>
import { computed, createVNode, onMounted, reactive, ref } from 'vue'
import { deleteUserUsingPost, getUserByIdUsingGet, userVoListByPageUsingPost } from '@/api/userController.ts'
import { message, Modal } from 'ant-design-vue'
import dayjs from 'dayjs'
import { ExclamationCircleOutlined } from '@ant-design/icons-vue'

const columns = [
  {
    title: 'id',
    dataIndex: 'id'
  },
  {
    title: '账号',
    dataIndex: 'userAccount'
  },
  {
    title: '用户名',
    dataIndex: 'userName'
  },
  {
    title: '头像',
    dataIndex: 'userAvatar'
  },
  {
    title: '简介',
    dataIndex: 'userProfile'
  },
  {
    title: '用户角色',
    dataIndex: 'userRole'
  },
  {
    title: '创建时间',
    dataIndex: 'createTime'
  },
  {
    title: '更新时间',
    dataIndex: 'updateTime'
  },
  {
    title: '操作',
    key: 'action'
  }
]

const dataList = ref([])
const total = ref(0)

const searchParams = reactive<API.UserQueryRequest>({
  current: 1,
  pageSize: 10
})

const fetchData = async () => {
  const res = await userVoListByPageUsingPost(searchParams)
  console.log(res.data)
  if (res.data.data) {
    dataList.value = res.data.data.records ?? []
    total.value = res.data.data.total ?? 0
  } else {
    message.error(res.data.message)
  }
}
const doSearch = async () => {
  searchParams.current = 1
  fetchData()
}

// 用户详情
const open = ref<boolean>(false)
const userData = ref({
  createTime: undefined,
  editTime: undefined,
  id: undefined,
  isDelete: undefined,
  updateTime: undefined,
  userAccount: undefined,
  userAvatar: undefined,
  userName: undefined,
  userPassword: undefined,
  userProfile: undefined,
  userRole: undefined
})
const showModal = async (id: String) => {

  if (id === null) {
    return
  }
  const res = await getUserByIdUsingGet({
    id: id
  })
  if (res.data.data && res.data.code === 0) {
    userData.value = res.data.data
    open.value = true
  } else {
    message.error(res.data.message)
  }
}

// 删除用户
const doDelete = async (id: String) => {

  Modal.confirm({
    title: '是否删除该用户',
    icon: createVNode(ExclamationCircleOutlined),
    content: '系统提示',
    okText: '确认',
    okType: 'danger',
    cancelText: '取消',
    async onOk() {
      const res = await deleteUserUsingPost({
        id: id
      })
      if (res.data.code === 0) {
        message.success('删除成功！')
        await fetchData()
      } else {
        message.error('删除失败!')
      }
    },
    onCancel() {
    }
  })
}

// 分页参数
const pagination = computed(() => {
  return {
    current: searchParams.current ?? 1,
    pageSize: searchParams.pageSize ?? 10,
    total: total.value,
    showSizeChanger: true,
    showTotal: (total) => `共 ${total} 条`
  }
})
// 表格变化处理
const doTableChange = (page: any) => {
  searchParams.current = page.current
  searchParams.pageSize = page.pageSize
  fetchData()
}

onMounted(() => {
  fetchData()
})
</script>

